package com.wx.tgp.designpattern.FactoryModePattern.SimpleFactory;

import java.util.Scanner;

/**
 * 计算器，利用简单工厂模式模拟计算器的，加减乘除
 */
public class Calculator {

    public static void main(String args[]){
        int num1 = 0;
        int num2 = 0;
        String str3 = "";
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("请输入第一个数：");
            String str1 = scanner.nextLine();
            try{
                num1 = Integer.parseInt(str1);
            } catch (Exception e){
                System.out.println("不能输入字符，请重新输入");
                continue;
            }
            break;
        }
        while(true){
            System.out.println("请输入运算字符('+','-','*','/')：");
            str3 = scanner.nextLine();
            if("+".equals(str3)||"-".equals(str3)||"*".equals(str3)||"/".equals(str3)){
                break;
            }else{
                continue;
            }
        }
        while(true){
            System.out.println("请输入第二个数");
            String str2 = scanner.nextLine();
            try{
                num2 = Integer.parseInt(str2);
                if("/".equals(str3)){
                    if(num2==0){
                        System.out.println("除数不能为0，请重新输入");
                        continue;
                    }
                }
            } catch (Exception e){
                System.out.println("不能输入字符，请重新输入");
                continue;
            }
            break;
        }
//        FactoryOperation fo = new FactoryOperation();
        FactoryOperation fo = null;
        try{
           Class className =  FactoryOperation.class;
            fo = (FactoryOperation)className.newInstance();
        }catch (Exception e){
            e.printStackTrace();
        }
        Operation operation = fo.getOperation(num1,num2,str3);
        double result = operation.getResult();
        System.out.println(num1 + " "+str3+" " + num2 + " = " + result);
    }
}
